<?php 
	class FilmAction{
			var $ipsclass;
			var $common;
			var $profile_url;
			var $custom_fields;
			var $defaultPerPage = 10;
			function auto_run(){
				
			}
			/*
			 * load categories
			 */ 
			function getCategories($sortByLang="vn", $sortType = "DESC", $startPage = 0, $perPage = "10"){
				if(intval($startPage) < 0 ){
					$startPage = 0;
				}
				if($perPage <= 0){
					$perPage = $this->defaultPerPage;
				}
				$cats= array();
				$where = "1=1 order by c.category_name_{$sortByLang} {$sortType}";
				$this->ipsclass->DB->build_query( array('select' => 'c.*',
													'from'   => 'film_category c',
													'where'    => $where
													) 
											);
				$this->ipsclass->DB->exec_query();
				while($row = $this->ipsclass->DB->fetch_row()){
					if(intval($row['category_id']) > 0 ){
						$row['rewriteCatNameVN'] = $this->common->filter_title($row['category_name_vn']);
						$row['rewriteCatNameUS'] = $this->common->filter_title($row['category_name_us']);
						$cats[] = $row;
					}
				}
				return $cats;
			}
			
			function loadFilms($filmCat, $sortByLang="vn", $sortType = "DESC", $startPage = 0, $perpage = "10", $loadNews = FALSE){
				if(intval($startPage) < 0 ){
					$startPage = 0;
				}
				if($perPage <= 0){
					$perPage = $this->defaultPerPage;
				}
				$where = "f.film_category_id = {$filmCat} and 1=1 order by f.film_name_{$sortByLang} {$sortType} ;";
				
				$this->ipsclass->DB->build_query( array('select' => 'count(f.film_id) as count',
                                                'from'   => 'film f',
                                                'where'    => $where
                                                ) 
                                        );
				$this->ipsclass->DB->exec_query();
				$totalId = $this->ipsclass->DB->fetch_row();
				//var_dump($totalId);exit();
				if($totalId['count'] <= 0){
					return null;
				}
				$this->ipsclass->DB->flush_query();
				$this->ipsclass->DB->build_query( array( 'select'   => 'f.*',
														 'from'     => array( 'film' => 'f' ),
														 'where'    => $where,
														 'limit'         => array($start, $perPage),
														 'add_join' => array(0 => array(
																						 'select'=>'c.*',
																						 'from'   => array( 'film_category' => 'c' ),
																						 'where'    => 'c.category_id = f.film_category_id',
																						 'type'   => 'left'
																						),
																			  1 => array( 'select' => 'fc.*',
																							  'from'   => array( 'film_chapter' => 'fc' ),
																							  'where'  => 'fc.film_id=f.film_id',
																							  'type'   => 'left' ),
																			 2 => array( 'select' => 'fr.*',
																							  'from'   => array( 'film_rate' => 'fr' ),
																							  'where'  => 'fr.film_id = f.film_id',
																							  'type'   => 'left' ),
																			  3 => array( 'select' => 'sf.*',
																							  'from'   => array( 'sub_film' => 'sf' ),
																							  'where'  => 'sf.film_id = f.film_id',
																							  'type'   => 'left' )
														 )
													)
												);
				$this->ipsclass->DB->exec_query();
				$temps = array();
				while($row = $this->ipsclass->DB->fetch_row()){
					$row['rewriteCatNameVN'] = $this->common->filter_title($row['category_name_vn']);
					$row['rewriteCatNameUS'] = $this->common->filter_title($row['category_name_us']);
					$row['rewriteFilmNameVN'] = $this->common->filter_title($row['film_name_us']);
					$row['rewriteFilmNameUS'] = $this->common->filter_title($row['film_name_us']);
					$row['rewriteFilmCreateDate'] = date("{$this->ipsclass->lang['seoDateFormat']}", $row['film_create_date']);
					$temps[] = $row;
					
				}
				$datas_temp = array();
				foreach ($temps as $row) {
					$datas_temp[] = $row;
				}
				$datas = array();
				//var_dump($loadNews) ;exit();
				if($loadNews){
					$page = $this->ipsclass->buildMyPageLinks( array(  'TOTAL_POSS'  => $totalId['count'],
																   'PER_PAGE'    => $perPage,
																   'CUR_ST_VAL'  => $start,
																   'L_SINGLE'    => "",
																   'L_MULTI'     => $this->ipsclass->lang['tpl_pages'],
																   'BASE_URL'    => "#"
																)
															);
															//var_dump($page);exit();
					$datas['page'] = $page;
				}
				$datas['totalData'] = count($datas_temp);
				$datas['datas'] = $datas_temp;
				return $datas;
			}
			
			function getFilmsById($filmId){
				$films= array();
				$where = "f.film_id = {$filmId} and 1=1 ;";
				$this->ipsclass->DB->build_query( array('select' => 'f.*',
													'from'   => 'film f',
													'where'    => $where
													) 
											);
				$this->ipsclass->DB->exec_query();
				$films = $this->ipsclass->DB->fetch_row();
				if(intval($films['film_id']) > 0 ){
					$row['rewriteFilmNameVN'] = $this->common->filter_title($row['film_name_us']);
					$row['rewriteFilmNameUS'] = $this->common->filter_title($row['film_name_us']);
					$row['rewriteFilmCreateDate'] = date("{$this->ipsclass->lang['seoDateFormat']}", $row['film_create_date']);
				}				
				return $films;
			}
			
			function getChapters($film, $sortByLang="vn", $sortType = "DESC"){
				if(intval(film) <=0){
						return null;
				}
				$chapters= array();
				$where = " fc.film_id = {film} and 1=1 order by fc.chap_film_file_name {$sortType}";
				$this->ipsclass->DB->build_query( array('select' => 'fc.*',
													'from'   => 'film_chapter fc',
													'where'    => $where
													) 
											);
				$this->ipsclass->DB->exec_query();
				while($row = $this->ipsclass->DB->fetch_row()){
					if(intval($row['chap_film_id']) > 0 ){
						$chapters[] = $row;
					}
				}
				return $chapters;
			}
			
			/*
			 * get all film rate by film id
			 */ 
			 function getFilmRates($film, $sortByLang="vn", $sortType = "DESC"){
				if(intval(film) <=0){
						return null;
				}
				$rates= array();
				$where = " fr.film_id = {film} and 1=1 order by fr.film_rate_id {$sortType}";
				$this->ipsclass->DB->build_query( array('select' => 'fr.*',
													'from'   => 'film_rate fr',
													'where'    => $where
													) 
											);
				$this->ipsclass->DB->exec_query();
				while($row = $this->ipsclass->DB->fetch_row()){
					if(intval($row['film_rate_id']) > 0 ){
						$rates[] = $row;
					}
				}
				return $rates;
			}
			
			/*
			 * get all film rate by film id
			 */ 
			 function getFilmsubs($film, $sortByLang="vn", $sortType = "DESC"){
				if(intval(film) <=0){
						return null;
				}
				$subtitles= array();
				$where = " sf.film_id = {film} and 1=1 order by sf.sub_film_id {$sortType}";
				$this->ipsclass->DB->build_query( array('select' => 'sf.*',
													'from'   => 'sub_film sf',
													'where'    => $where
													) 
											);
				$this->ipsclass->DB->exec_query();
				while($row = $this->ipsclass->DB->fetch_row()){
					if(intval($row['film_rate_id']) > 0 ){
						$subtitles[] = $row;
					}
				}
				return $subtitles;
			}
			
			function init(){
				
				
			}
	}
?>
